package com.pickgliss.bitmapEngin
{
	import com.pickgliss.bitmapEngin.ds.CacheClipList;
	import flash.utils.Dictionary;
	public class BitmapRenderEngine
	{
		
		private static var _instance:BitmapRenderEngine

		public static function get Instance():BitmapRenderEngine
		{
			if(_instance == null)
			{
				_instance = new BitmapRenderEngine();
			}
			return _instance;
		}

		public function BitmapRenderEngine()
		{
			_layers = new Vector.<BitmapRenderLayer>();
		}

		private var _layers:Vector.<BitmapRenderLayer>;
		
		public function addLayer($id:String,$width:int,$height:int,$renderCopy:Boolean = true,$smoothing:Boolean = false):BitmapRenderLayer
		{
			removeLayer($id);
			var layer:BitmapRenderLayer = new BitmapRenderLayer($id,$width,$height,$renderCopy,$smoothing);
			_layers.push(layer);
			return layer;
		}
		
		public function addListToLayer($id:String,$list:CacheClipList):void
		{
			var layer:BitmapRenderLayer = getLayer($id);
			layer.addRenderList($list);
		}
		
		public function getLayer($id:String):BitmapRenderLayer
		{
			for(var i:int = 0;i<_layers.length;i++)
			{
				if(_layers[i].id == $id)return _layers[i];
			}
			return null;
		}
		
		public function removeLayer($id:String):void
		{
			for(var i:int = 0;i<_layers.length;i++)
			{
				if(_layers[i].id == $id)_layers.splice(i,1);
			}
		}
		
		public function removeListFromLayer($id:String,$list:CacheClipList):void
		{
			var layer:BitmapRenderLayer = getLayer($id);
			layer.removeRenderList($list);
		}
		
		public function render():void
		{
			for(var i:int = 0;i<_layers.length;i++)
			{
				_layers[i].render();
			}
		}
		
		public function clearAll():void
		{
			for(var i:int = 0;i<_layers.length;i++)
			{
				_layers[i].dispose();
			}
			_layers = null;
		}
	}
}